package ar.edu.unlu.sistemas.p2p.android;

import java.util.Collection;
import java.util.Iterator;

import android.content.Intent;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;
import ar.edu.unlu.sistemas.p2p.business.vo.DocumentVO;
import ar.edu.unlu.sistemas.p2p.business.vo.TermFrecuencyVO;
import ar.edu.unlu.sistemas.p2p.business.vo.WordVO;

public class IndexActivity extends AbsP2PActivity implements OnClickListener{
	
	/** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.index);
        Button btn = (Button)findViewById(R.id.button_find);
        btn.setOnClickListener(this);
        RadioButton rbtn = (RadioButton)findViewById(R.id.radio_button_doc);
        rbtn.setOnClickListener(this);
        rbtn = (RadioButton)findViewById(R.id.radio_button_word);
        rbtn.setOnClickListener(this);
        rbtn = (RadioButton)findViewById(R.id.radio_button_tf);
        rbtn.setOnClickListener(this);
        bindService(new Intent(IndexActivity.this,AppService.class), mConnection, BIND_AUTO_CREATE);
    }
    
    public void onClick(View v) {
    	EditText edt;
    	switch (v.getId()) {
		case R.id.button_find:
			RadioGroup rg = (RadioGroup) this.findViewById(R.id.radio_group_table);
			int sel = rg.getCheckedRadioButtonId();
			switch (sel) {
				case R.id.radio_button_doc:
					edt = (EditText) this.findViewById(R.id.edit_text_find_1);
					this.findDocs(edt.getText().toString());
					edt.setText(null);
					break;
				case R.id.radio_button_word:
					edt = (EditText) this.findViewById(R.id.edit_text_find_1);
					this.findWords(edt.getText().toString());
					edt.setText(null);
					break;
				case R.id.radio_button_tf:
					edt = (EditText) this.findViewById(R.id.edit_text_find_1);
					String doc = edt.getText().toString();
					edt.setText(null);
					edt = (EditText) this.findViewById(R.id.edit_text_find_2);
					String word = edt.getText().toString();
					edt.setText(null);
					this.findTFs(doc, word);
					break;
				default:
					break;
			}
			break;
		case R.id.radio_button_doc:
			edt = (EditText) this.findViewById(R.id.edit_text_find_1);
			edt.setVisibility(View.VISIBLE);
			edt.setHint("Documento");
			edt = (EditText) this.findViewById(R.id.edit_text_find_2);
			edt.setVisibility(View.GONE);
			break;
		case R.id.radio_button_word:
			edt = (EditText) this.findViewById(R.id.edit_text_find_1);
			edt.setVisibility(View.VISIBLE);
			edt.setHint("Termino");
			edt = (EditText) this.findViewById(R.id.edit_text_find_2);
			edt.setVisibility(View.GONE);
			break;
		case R.id.radio_button_tf:
			edt = (EditText) this.findViewById(R.id.edit_text_find_1);
			edt.setVisibility(View.VISIBLE);
			edt.setHint("Documento");
			edt = (EditText) this.findViewById(R.id.edit_text_find_2);
			edt.setVisibility(View.VISIBLE);
			edt.setHint("Termino");
			break;
    	default:
    		break;
    	}
    }
    
    private void findWords(String word) {
    	LinearLayout ll = (LinearLayout) this.findViewById(R.id.layout_query_result);
		ll.removeAllViews();
    	Collection<WordVO> coll = this.appService.findWords(word);
    	Iterator<WordVO> it = coll.iterator();
    	WordVO vo;
		while (it.hasNext()) {
			vo = it.next();
			TextView tv = new TextView(this);
			//tv.setTextColor(Color.BLACK);
			tv.setTextSize(30);
			tv.setText(vo.getName());
			tv.setOnClickListener(this);
			//tv.setBackgroundColor(Color.rgb(176+m, 176+m, 176+m));
			tv.setGravity(Gravity.CENTER_HORIZONTAL);
			ll.addView(tv);
		}
    }
    
    private void findDocs(String doc) {
    	LinearLayout ll = (LinearLayout) this.findViewById(R.id.layout_query_result);
		ll.removeAllViews();
    	Collection<DocumentVO> coll = this.appService.findDocs(doc);
    	Iterator<DocumentVO> it = coll.iterator();
    	DocumentVO vo;
		while (it.hasNext()) {
			vo = it.next();
			TextView tv = new TextView(this);
			//tv.setTextColor(Color.BLACK);
			tv.setTextSize(30);
			tv.setText(vo.getName());
			tv.setOnClickListener(this);
			//tv.setBackgroundColor(Color.rgb(176+m, 176+m, 176+m));
			tv.setGravity(Gravity.CENTER_HORIZONTAL);
			ll.addView(tv);
		}
    }
    
    private void findTFs(String doc, String word) {
    	LinearLayout ll = (LinearLayout) this.findViewById(R.id.layout_query_result);
		ll.removeAllViews();
    	Collection<TermFrecuencyVO> coll = this.appService.findTFs(doc, word);
    	Iterator<TermFrecuencyVO> it = coll.iterator();
    	TermFrecuencyVO vo;
		while (it.hasNext()) {
			vo = it.next();
			TextView tv = new TextView(this);
			//tv.setTextColor(Color.BLACK);
			tv.setTextSize(30);
			tv.setText(vo.toString());
			tv.setOnClickListener(this);
			//tv.setBackgroundColor(Color.rgb(176+m, 176+m, 176+m));
			tv.setGravity(Gravity.CENTER_HORIZONTAL);
			ll.addView(tv);
		}
    }

}
